package Database;

import TomcatRelated.UserProfile;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.provider.Settings.Secure;

public class Database
{

	//***************************************Class Variables*******************************************//

	private static Database _database = null;
	private UserProfile _userPro;
	//***************************************Constructors*******************************************//

	private Database(Context context)
	{
		init_userPro(context);
	}

	public static Database getInstance(Context context)
	{
		if(_database == null)
		{
			_database = new Database(context);
		}
	
		return _database;
	}

	//***************************************Public Methods*******************************************//

	public String get_device_id() 
	{
		return _userPro.get_android_id();
	}
	
	public UserProfile getUserProfile(){
		return this._userPro;
	}
	
	public void set_userProfile_google_account(String googleAcount) 
	{
		_userPro.set_google_account(googleAcount);
	}
	
	
	public void set_userProfile_c2dm_register_id(String c2dmstring) 
	{
		_userPro.set_c2dm_register_id(c2dmstring);
	}

	//***************************************Private Methods*******************************************//

	private void init_userPro(Context context) 
	{
		this._userPro = new UserProfile();
		//TODO - change the android_id with the google account
		//this._userPro.set_android_id(Secure.getString(context.getContentResolver(), Secure.ANDROID_ID));
		this._userPro.set_android_id(getGoogleAcount(context));
		
		//WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
		//this._device_id = wm.getConnectionInfo().getMacAddress();
		//TelephonyManager telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		//this._device_id = telManager.getDeviceId();			
	}
	
	private String getGoogleAcount(Context context)
	{
		Account[] listOfAccounts = (AccountManager.get(context).getAccountsByType("com.google"));

		if (listOfAccounts.length == 0)
		{
			return null;
		}
		else
		{
			Account googleAccount = (AccountManager.get(context).getAccountsByType("com.google"))[0];
			return googleAccount.name;
		}
	}
	
}
